package com.liwang.lanouhn.nvshengo.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.liwang.lanouhn.nvshengo.Info.VideoInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.adapter.ViewPagerAdaper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 2016/10/22.
 */
public class NumberListFragment extends Fragment {

    private RadioButton top_rg_a;
    private RadioButton top_rg_b;
    private RadioGroup main_top_rg;
    private ViewPager main_viewpager;
    private List<Fragment> fragments = new ArrayList<>();
    public static NumberListFragment newInstants(VideoInfo info) {
        NumberListFragment numberListFragment = new NumberListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("videoinfo", info);
        numberListFragment.setArguments(bundle);
        return numberListFragment;
    }
    private VideoInfo info;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_numberlist, null);

        initView(view);
        initEvent();
        return view;
    }

    private void initView(View view) {
        info= (VideoInfo) getArguments().getSerializable("videoinfo");
        top_rg_a = (RadioButton) view.findViewById(R.id.top_rg_a);
        top_rg_b = (RadioButton) view.findViewById(R.id.top_rg_b);
        main_top_rg = (RadioGroup) view.findViewById(R.id.main_top_rg);
        main_viewpager = (ViewPager) view.findViewById(R.id.main_viewpager);
    }

    private void initEvent() {
        WeekContribute week =WeekContribute.newInstants(info);
        DayContribute day =DayContribute.newInstants(info);
        fragments.add(week);
        fragments.add(day);
        main_viewpager.setAdapter(new ViewPagerAdaper(getFragmentManager(), fragments));
        /**
         * 为 Viewpager 设置页面切换监听，当页面切换完成被选中时，我们同步 RadioButton 的状态
         **/
        main_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) top_rg_a.setChecked(true);
                else top_rg_b.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        /**
         * 为 RadioGroup 设置选中变化事件监听，当 RadioButton 状态变化，我们同步 Viewpager 的选中页面
         **/
        main_top_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == top_rg_a.getId()) main_viewpager.setCurrentItem(0);
                else if (checkedId == top_rg_b.getId()) main_viewpager.setCurrentItem(1);
            }
        });

        //设置默认选中页
        main_viewpager.setCurrentItem(0);
    }
    }


